package class1;

public class Course {
    private String courseName;
    private int courseId;
    private String teacher;
    private String place;
    private String time;
    private Student[] students;

    public Course(int courseId, String courseName, String string, String place, String time) {
        super();
        this.courseId = courseId;
        this.courseName = courseName;
        this.place = place;
        this.time = time;
        this.setTeacher(string);
        students = new Student[30];
    }

    public Course(int courseId, String courseName, String place, String time) {
        super();
        this.courseId = courseId;
        this.courseName = courseName;
        this.place = place;
        this.time = time;
        students = new Student[30];
    }

    public Course(int courseId, String courseName, String place) {
        super();
        this.courseId = courseId;
        this.courseName = courseName;
        this.place = place;
        students = new Student[30];
    }

    public Course(int courseId, String courseName) {
        super();
        this.courseId = courseId;
        this.courseName = courseName;
        students = new Student[30];
    }

    public Course() {
        super();
        students = new Student[30];
    }

    // 修改或获取属性值id，name，credit，
    public int getId() {
        return this.courseId;
    }

    public void setId(int id) {
        this.courseId = id;
    }

    public String getName() {
        return this.courseName;
    }

    public void setName(String name) {
        this.courseName = name;
    }

    public String getTeacher() {
        return teacher;
    }

    public void setTeacher(String string) {
        this.teacher = string;
    }

    public String getPlace() {
        return this.place;

    }

    public void setPlace(String place) {
        this.place = place;
    }

    public String getTime() {
        return this.time;

    }

    public void setTime(String time) {
        this.time = time;
    }

    // 课加入学生
    public boolean addStudent(Student stu) {
        boolean flag = false;// 标志值：是否加入成功
        // 如果学生没有选过这门课，同时课的学生还没满则执行
        if (!isSelectedStudent(stu) && isNullStudent(stu)) {
            for (int i = 0; i < students.length; i++) {
                if (students[i] == null) {
                    students[i] = stu;
                    flag = true;
                    break;
                }
            }
        }
        return flag;
    }

    // 课移除学生
    public boolean removeStudent(Student stu) {
        boolean flag = false;
        if (isSelectedStudent(stu)) {// 选过这门课
            for (int i = 0; i < students.length; i++) {
                if (students[i] == stu) {
                    students[i] = null;
                    flag = true;
                    break;
                }
            }
        }
        return flag;
    }

    public void displayStudent() {
        System.out.println("选择的课程：" + this.courseName + "的学生有:");
        for (Student s : students) {
            if (s != null) {
                System.out.print(s.getStuName() + " ");
            }
        }
        System.out.println();
    }

    // 子方法1：学生是否选过这门课
    public boolean isSelectedStudent(Student stu) {
        boolean flag = false;
        for (Student s : students) {
            if (s == stu) {
                flag = true;
                break;
            }
        }
        return flag;
    }

    // 子方法2：学科学生未达到限定人数吗
    public boolean isNullStudent(Student stu) {
        boolean flag = false;
        for (Student s : students) {
            if (s == null) {
                flag = true;
                break;
            }
        }
        return flag;
    }

}